$include("$theme/feedDetail.css");

with ($ns("fp.renderers"))
{
    fp.renderers.FeedDetailRenderer = function()
    {
        var me = this
        var base = {};
        
        me.renderFeed = function(p_feed, $feed)
        {
            if ($feed == null)
            {
                $feed = $("<div/>");
            }
            
            try
            {
                $feed.addClass("feedDetail");
                $feed.data("feed", p_feed);
                $feed.attr("id", p_feed.id);
                _renderHead(p_feed, $feed);
                _renderContent(p_feed, $feed);
            }
            catch (e)
            {
                console.error(e.toString());
            }
            return $feed;
        };
        
        function _renderHead(p_feed, $feed)
        {
            var $head = $("<div id='head'>");
            
            var $star = $("<span id='star'>");
            $star.attr("title", $msg("fp", "add_star"));
            $star.click(_star_click);
            var starred = false;
            if (typeof(p_feed.starred) != "undefined")
            {
                starred = p_feed.starred;
            }
            else
            {
                for (var i = 0; i < p_feed.categories.length; i++)
                {
                    if (p_feed.categories[i].endsWith("/state/com.google/starred"))
                    {
                        p_feed.starred = true;
                        starred = true;
                        break;
                    }
                }
            }

            if (starred)
            {
                $feed.addClass("starred");
                $star.attr("title", $msg("fp", "remove_star"));
            }
            $head.append($star);
            
            var $title = $("<div id='title' />");
            var $link = $("<a target='_blank'>");
            if (p_feed.alternate != null && p_feed.alternate.length > 0)
            {
                $link.attr("href", p_feed.alternate[0].href);
            }
            $link.html(p_feed.title ? p_feed.title : ".");
            $title.append($link);
            $head.append($title);
            
            var $publishTime = $("<div id='publishTime' />");
            $publishTime.text(fp.utils.DateTimeUtil.getDateTimeString(p_feed.crawlTimeMsec));
            $head.append($publishTime);
            
            var $author = $("<div id='author' />");
            $author.text(p_feed.author ? p_feed.author : ".");
            $head.append($author);
            
            var $origin = $("<span id='origin' />");
            var $icon = $("<img>");
            $icon.attr("src", $mappath("$google/s2/favicons?domain_url=") + p_feed.origin.htmlUrl);
            $origin.append($icon);
            var $link = $("<a target='_blank' />");
            $link.attr("href", p_feed.origin.htmlUrl);
            $link.text(p_feed.origin.title ? p_feed.origin.title : ".");
            $origin.append($link);
            $head.append($origin);
            $feed.append($head);
        }
        
        function _renderContent(p_feed, $feed)
        {
            var $content = $("<div id='content' />");

            $content.html(fp.utils.ContentExtractor.extractContent(p_feed));
            
            $feed.append($content);
            $content.find("a").attr("target", "_blank");
        }
        
        
        
        
        function _star_click(e)
        {
            var $img = $(this);
            var $e = $img.closest(".feedDetail");
            var feed = $e.data("feed");
            if ($e.hasClass("starred"))
            {
                feed.starred = false;
                g.Reader.removeStar(feed);
                $e.removeClass("starred");
                $img.attr("title", $msg("fp", "add_star"));
            }
            else
            {
                feed.starred = true;
                g.Reader.addStar(feed);
                $e.addClass("starred");
                $img.attr("title", $msg("fp", "remove_star"));
            }
        }
        
        return me;
    };
}